package com.leo.ktvplayer.util;

import android.util.Log;




import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import javax.xml.parsers.SAXParserFactory;



/**
 *
 */
public class XmlFormatUtil {

    public static <T> T xmlToObj(String resouce ,Class<T> clz){
        String result =  replace(resouce);
        return JsonUtil.jsonStrToObj(clz, paseXml(result.trim()));
    }

    private static String replace(String data){
        data = data.replace("<![CDATA[","");
        data = data.replace("]]>","");
        data = data.replace("<xml>","");
        data = data.replace("</xml>","");
        return data;
    }
    private static String paseXml(String current) {
        String[] spliteArray = current.split("</");
        for (int i = 0; i < spliteArray.length - 1; i++) {
            String result = spliteArray[i];
            String tag = result.substring(result.lastIndexOf("<") + 1, result.lastIndexOf(">"));
            String content = result.substring(result.lastIndexOf(">") + 1, result.length());
            if (i == 0) {//如果是第一条
                builder.append("{\"" + tag + "\":\"" + content + "\",");
            } else if (i == spliteArray.length - 2) {//如果是最后
                builder.append("\"" + tag + "\":\"" + content + "\"}");;
            } else {
                builder.append("\"" + tag + "\":\"" + content + "\",");
            }
        }
        String str = new String(builder.toString());
        Log.d("acthome",str);
        builder.setLength(0);//清空字符传;
        return str;
    }

    private static StringBuilder builder = new StringBuilder();
}
